//
//  GXOtherTableViewController.swift
//  BallGame
//
//  Created by GuangXiao on 16/2/23.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import LocalAuthentication
class GXOtherTableViewController: UITableViewController {

    /// 存放cell数据的数组
    var cellsArr : NSArray = {

         let path = NSBundle.mainBundle().pathForResource("Other", ofType: "plist")
        let cellsArr = NSArray(contentsOfFile: path!)
        
        return cellsArr!
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    
    /**
     返回多少行
     */
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.cellsArr.count
    }

    
    /**
     cell长啥样
     */
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        //获取数据
        let cellData = self.cellsArr[indexPath.row] as! NSDictionary
        
        //注册cell
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
        //缓冲池寻找cell
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        //设置文字和头像
        cell.textLabel?.text = cellData["title"] as? String
        let imageName = cellData["icon"] as? String
        cell.imageView?.image = UIImage(named: imageName!)
        
        return cell
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cellData = self.cellsArr[indexPath.row] as? NSDictionary
        let isFinger = cellData!["fingerPrint"] as! Bool
//        if (sizeofValue(cellData!["targetViewController"] as? String) > 0){
//            print("有目标控制器")
//        }else{
//            print("没哟目标控制器")
//        }
        let title = cellData!["title"] as! String
        if let controllerName = cellData!["targetViewController"] as? String{
            if isFinger {
                print("要指纹验证")
                fingerPrintVerify(controllerName,title: title)
            }else{
                print("不验证 直接跳转")
                switchController(controllerName,title: title)
            }
        }
        
        
//        print("\(indexPath.row)被点击了")
    }
    
    /**
     指纹识别的方法
     */
    private func fingerPrintVerify(controllerName : String,title : String){
        //1.指纹识别上下文
        let context = LAContext()
        //        print(NSThread.currentThread())
        
        //2.判断指纹识别是否可用
        if context.canEvaluatePolicy(.DeviceOwnerAuthentication, error: nil){
            context.evaluatePolicy(.DeviceOwnerAuthentication, localizedReason: "指纹识别更安全", reply: { (_, error) -> Void in
                
                if error != nil {
                    print("指纹识别错误")
                }else {
                    //将UI 放到主线程中
                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        print("指纹识别成功,跳转控制器")
                        //                        print(NSThread.currentThread())
                        self.switchController(controllerName,title: title)
                    })
                }
            })
        }

    }
    
    private func switchController(controllerName : String,title : String){
        
        if controllerName.isEmpty{
            print("没有目标控制器")
        }else{
            print("有目标控制器")
            let VCName = "BallGame." + controllerName
           
            let classVC = NSClassFromString(VCName) as! UIViewController.Type
            let vc = classVC.init()
            
            
            self.navigationController?.pushViewController(vc, animated: true)
            vc.navigationItem.title = title
            
            
        }
        
        
//        let gameTwoVc = GXViewController()
//        self.navigationController?.pushViewController(gameTwoVc, animated: true)
    }
    
    
    
    
    /**
     行高
     */
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 60
    }

    
    
    }
